package AutoJavaDoc;

$OPEN_COMMENT  = "/**";
$CLOSE_COMMENT = " */";
$COMMENT_START = " * ";
$linebreak = "\n";

=begin
  sub: checkClass
=cut
sub checkClass { my ( $self, $inLine, $braceCount, $currentBrace, $inAuthor, $inVersion ) = @_;
	# CHECK FOR CLASS...
	if (    $inLine =~ m/class/
		 && $inLine !~ m!//.*class!
		 && ( $braceCount - $currentBrace ) < 1 )
	{
		$Main::isClass = 1;
		# Grab indent
		$inLine =~ m/^([\s]*)[^\s]/s;
		$docIndent = $1;
		
		$blankComment = $docIndent . $COMMENT_START;

		# strip open brace
		$inLine =~ s/{//;

		# Get the className
		# match [words or the "."] after "class "
		$inLine =~ m/class ([\w.]*)/;
		$className = $1;

		# Strip whitespace
		$inLine =~ s/\s//g;

		# Create the doc...
		$lineDoc = $docIndent . $linebreak . $docIndent . $OPEN_COMMENT . $linebreak;
		$lineDoc .= $docIndent . $COMMENT_START . $className . " " . $TODO . $linebreak;
		$lineDoc .= $blankComment . $linebreak;
		$lineDoc .= $blankComment . '@usage <pre>' . $linebreak;
		$lineDoc .= $blankComment . $linebreak;
		$lineDoc .= $blankComment . '</pre>' . $linebreak;
		$lineDoc .= $blankComment . $linebreak;
		$lineDoc .= $blankComment . '@example <pre>' . $linebreak;
		$lineDoc .= $blankComment . $linebreak;
		$lineDoc .= $blankComment . '</pre>' . $linebreak;
		$lineDoc .= $blankComment . $linebreak;
		
		if($inAuthor) {
			$lineDoc .= $blankComment . '@author ' . $inAuthor . $linebreak;
		}

		if($inVersion) {
			$lineDoc .= $blankComment . '@verson ' . $inVersion . $linebreak;
		}

		# End the class comment.
		$lineDoc .= $docIndent . $CLOSE_COMMENT . $linebreak;
		return $lineDoc;
	}
}
=begin
 sub: checkClassProperty
=cut
sub checkClassProperty { my ( $self, $inLine, $braceCount, $currentBrace, $onlyPublic ) = @_;

	#CHECK FOR CLASS PROPERTIES...
	if (    $inLine =~ m/var/
		 && $inLine !~ m!//.*var!
		 && ( $braceCount - $currentBrace ) < 2
		 && $Main::isClass == 1 )
	{
		
		# Calculate indent
		$inLine =~ m/^([\s]*)[^\s]/s;
		$docIndent = $1;

		# remove ;
		$inLine =~ s/;//;

		# check member attributes
		$isPrivate = $inLine =~ m/private.*var/g;
		$isPublic  = $inLine =~ m/public.*var/g;
		$isStatic  = $inLine =~ m/static.*var/g;
		
		if(($isPublic && $onlyPublic) || not $onlyPublic)
		{	
			$inLine =~ m/^.*var(.*)/;
			$varString = $1;
	
			# strip whitepace
			$varString =~ s/\s//g;
	
			# grab the varName
			@varName = split( m/:/, $varString );
			
			( $varName, $dataType ) = @varName;
			# $dataType isn't used at the moment - still, we have it anyway.
			#    As long as it was defined.... 
			#    In AS3 this could also be * for loose typing.
			
			$varName =~ s/=.*//g;

			$lineDoc = $docIndent . $linebreak . $docIndent . $OPEN_COMMENT . $linebreak;
			$lineDoc .= $docIndent . $blankComment . $varName . " " . $TODO . $linebreak;
			$lineDoc .= $docIndent . $CLOSE_COMMENT . $linebreak;			
			
			return $lineDoc;
		}
	}
}
=begin
  sub: checkClassMethod
=cut
sub checkClassMethod { 
	my ( $self, $inLine, $braceCount, $currentBrace, $onlyPublic ) = @_;
	if (    $inLine =~ m/function/
		 && $inLine !~ m!//.*function!
		 && ( $braceCount - $currentBrace ) < 2
		 && $Main::isClass == 1 )
	{
		# Calculate indent
		$inLine =~ m/^([\s]*)[^\s]/s;
		$docIndent = $1;
		#check for identifiers
		$isPrivate = $inLine =~ m/private.*function.*\(/g;
		$isPublic  = $inLine =~ m/public.*function.*\(/g;
		$isStatic  = $inLine =~ m/static.*function.*\(/g;

		if(($isPublic && $onlyPublic) || not $onlyPublic)
		{	
			$inLine =~ s/{//g;
			
			$blankComment = $docIndent . $COMMENT_START;
			
			# strip whitespace
			$inLine =~ s/\s//g;
	
			# extract Method Name
			$inLine =~ m/^.*function(.*)\(/;
			$methodName = $1;
	
			# extract parameters string
			$inLine =~ m/^.*\((.*)\)/;
			$parameters = $1;
	
			# create parameters documentation stub
			$parametersList = "";
			@params = split ",", $parameters;
			for $param (@params) {    # any LIST expression
				@farg = split ":", $param;
				$parametersList .=
				  $blankComment . '@param ' . $farg[0] . " " . $TODO . $linebreak;
			}
	
			# extract return type
			$matchReturnType = $inLine =~ m/\):(.*)/;
			if ($matchReturnType) {
				$returnType = $blankComment . '@return ' . $1;
			}
			# Method comments begin...
			$lineDoc = $docIndent . $linebreak . $docIndent . $OPEN_COMMENT . $linebreak;
			$lineDoc .= $blankComment . $methodName . " " . $TODO . $linebreak;
	
			if ( $parametersList ne "" ) {
				$lineDoc .=  
					$blankComment . $linebreak . $parametersList;
			}
	
			if ($matchReturnType) {
				$lineDoc .= 
				  	$blankComment . $linebreak . $returnType . " " . $TODO . $linebreak;
			}
			$lineDoc .= $docIndent . $CLOSE_COMMENT . $linebreak;
						
			return $lineDoc;
		}
	}
}
=begin
  sub: checkInterface
=cut
sub checkInterface {
	my ( $self, $inLine, $braceCount, $currentBrace, $inAuthor, $inVersion ) = @_;
	if (    $inLine =~ m/interface/
		 && $inLine !~ m!//.*interface!
		 && ( $braceCount - $currentBrace ) < 1 )
	{
		$Main::isClass = 0;

		# Grab indent
		$inLine =~ m/^([\s]*)[^\s]/s;
		$docIndent = $1;

		$blankComment = $docIndent . $COMMENT_START;

		# strip open brace
		$inLine =~ s/{//;

		# Get the interfaceName
		# match [words or the "."] after "interface "
		$inLine =~ m/interface ([\w.]*)/;
		$interfaceName = $1;

		# Strip whitespace
		$inLine =~ s/\s//g;

		# Create the doc...
		$lineDoc = $docIndent . $linebreak . $docIndent . $OPEN_COMMENT . $linebreak;
		$lineDoc .= $docIndent . $COMMENT_START . $interfaceName . " " . $TODO . $linebreak;
		$lineDoc .= $blankComment . $linebreak;
		
		if($inAuthor) {
			$lineDoc .= $blankComment . '@author ' . $inAuthor . $linebreak;
		}
		
		if($inVersion) {
			$lineDoc .= $blankComment . '@verson ' . $inVersion . $linebreak;
		}

		# End the interface comment.
		$lineDoc .= $docIndent . $CLOSE_COMMENT . $linebreak;
		
	}
}
=begin
  sub: checkInterfaceMethod
=cut
sub checkInterfaceMethod {
	my ( $self, $inLine, $braceCount, $currentBrace ) = @_;
	if (    $inLine =~ m/function/
		 && $inLine !~ m!//.*function!
		 && ( $braceCount - $currentBrace ) < 2
		 && $Main::isClass == 0 )
	{
		# Calculate indent
		$inLine =~ m/^([\s]*)[^\s]/s;
		$docIndent = $1;

		$inLine =~ s/{//g;
		
		$blankComment = $docIndent . $COMMENT_START;
		
		# strip whitespace
		$inLine =~ s/\s//g;

		# extract Method Name
		$inLine =~ m/^.*function(.*)\(/;
		$methodName = $1;

		# extract parameters string
		$inLine =~ m/^.*\((.*)\)/;
		$parameters = $1;

		# create parameters documentation stub
		$parametersList = "";
		@params = split ",", $parameters;
		for $param (@params) {    # any LIST expression
			@farg = split ":", $param;
			$parametersList .=
			  $blankComment . '@param ' . $farg[0] . " - " . $TODO . $linebreak;
		}

		# extract return type
		$matchReturnType = $inLine =~ m/\):(.*)/;
		if ($matchReturnType) {
			$returnType = $blankComment . '@return ' . $1;
		}
		# Method comments begin...
		$lineDoc = $docIndent . $linebreak . $docIndent . $OPEN_COMMENT . $linebreak;
		$lineDoc .= $blankComment . $methodName . " " . $TODO . $linebreak;

		if ( $parametersList ne "" ) {
			$lineDoc .=  
				$blankComment . $linebreak
			  . $parametersList;
		}

		if ($matchReturnType) {
			$lineDoc .= 
			  	$blankComment . $linebreak
			  . $returnType . " " . $TODO . $linebreak;
		}
		$lineDoc .= $docIndent . $CLOSE_COMMENT . $linebreak;
		return $lineDoc;
	}
}
1;
